//
//  LoginViewController.m
//  AStage_shougongke
//
//  Created by 赵磊 on 16/1/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "aStageHeader.pch"
@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic, retain) UITextField *zhanghaoTextField;
@property (nonatomic, retain) UITextField *pwdTextField;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel *zhanghaoLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 100, 50, 30)];
    zhanghaoLabel.text = @"账号";
    [self.view addSubview:zhanghaoLabel];
    UILabel *pwdLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 160, 50, 30)];
    pwdLabel.text = @"密码";
    [self.view addSubview:pwdLabel];
    self.zhanghaoTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 30)];
    _zhanghaoTextField.placeholder = @"请输入账号";
    [self.view addSubview:_zhanghaoTextField];
    _zhanghaoTextField.borderStyle = UITextBorderStyleRoundedRect;
    _zhanghaoTextField.tag = 1000;
    _zhanghaoTextField.delegate = self;
    self.pwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 160, 200, 30)];
    _pwdTextField.placeholder = @"请输入密码";
    _pwdTextField.tag = 1001;
    _pwdTextField.borderStyle = UITextBorderStyleRoundedRect;
    _pwdTextField.delegate = self;
    [self.view addSubview:_pwdTextField];
    [self.view endEditing:YES];
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.layer.cornerRadius = 3;
    loginButton.clipsToBounds = YES;
    loginButton.backgroundColor = [UIColor redColor];
    loginButton.frame = CGRectMake(120, 215, 100, 30);
    [loginButton setTitle:@"登陆" forState:UIControlStateNormal];
    [self.view addSubview:loginButton];
    [loginButton addTarget:self action:@selector(enter) forControlEvents:UIControlEventTouchUpInside];
    UIButton *regButton = [UIButton buttonWithType:UIButtonTypeCustom];
    regButton.layer.cornerRadius = 3;
    regButton.clipsToBounds = YES;
    regButton.backgroundColor = [UIColor redColor];
    regButton.frame = CGRectMake(120, 270, 100, 30);
    [regButton addTarget:self action:@selector(registerAction) forControlEvents:UIControlEventTouchUpInside];
    [regButton setTitle:@"注册" forState:UIControlStateNormal];
    [self.view addSubview:regButton];
}
- (void)enter
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [[DataBaseManager shareInstance] openDB];
    NSMutableArray *dataArr = [[DataBaseManager shareInstance] select];
    for (Masseage *m in dataArr) {
        if ((m.mail == _zhanghaoTextField.text) && (m.pwd == _pwdTextField.text)) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
            [self dismissViewControllerAnimated:alert completion:^{
                
            }];
            [userDefaults setBool:YES forKey:@"isLogin"];
            [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timer) userInfo:nil repeats:NO];
            return;
        }
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的账号或密码有误" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}
- (void)timer
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)registerAction
{
    RegisterViewController *regVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:regVC animated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == 1000) {
        [textField resignFirstResponder];
        [[self.view viewWithTag:1001] becomeFirstResponder];
    } else {
        [textField resignFirstResponder];
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
